package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.come.Frame.ZhuFrame;
import org.come.bean.ChongjipackBean;
import org.come.bean.XXGDBean;
import org.come.entity.Goodstable;
import org.come.mouslisten.VipGoodsMouselistener;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.EquipTool;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.UserMessUntil;

import com.tool.btn.VipShopBtn;
import com.tool.tcpimg.RichLabel;
import com.tool.tcpimg.UIUtils;

/**
 * HGC-2019-11-21 连续充值物品面板
 * 
 * @author Administrator
 * 
 */
public class ContinuousRechargeGoodsJpanel extends JPanel {

	/**
	 * 物品Label
	 */
	private JLabel[] GoodsList;
	private JLabel[] goodsNums;
	private VipGoodsMouselistener[] mouselistener;
	/** 领取按钮 */
	private VipShopBtn getBtn;
	/** 达成条件 */

	private ChongjipackBean chongjipackBean;

	private RichLabel richLabel;

	/** 领取的总数量 */
	private int numMax;

	public ContinuousRechargeGoodsJpanel() {
		setPreferredSize(new Dimension(562, 60));
		setOpaque(false);
		setLayout(null);

		getGoodsNums();
		getGoodsList();
		getGetBtn();
		getRichLabel();
	}

	/**
	 * 兑换物品
	 */
	public void exchangeGoods() {
		if (chongjipackBean == null) {
			return;
		}
		if (GoodsListFromServerUntil.Surplussum("-1", "-1", numMax) < numMax) {
			ZhuFrame.getZhuJpanel().addPrompt2("你的背包不够");
			return;
		}
		String sendmes = Agreement.getAgreement().DayforweekgradesureAgreement("v" + chongjipackBean.getPackgradetype());
		SendMessageUntil.toServer(sendmes);
	}

	/**
	 * 展示数据
	 * 
	 * @param chongjipackBean
	 */
	public void showGoods(ChongjipackBean chongjipackBean) {
		this.chongjipackBean = chongjipackBean;
		if (chongjipackBean != null) {
			richLabel.setText("#W连续充值#R" + chongjipackBean.getPackgradetype() + "#W天");
			List<XXGDBean> goods = ChongjipackBean.getGoods(chongjipackBean.getPackgoods());
			numMax = 0;
			changeView();
			for (int i = 0; i < goods.size(); i++) {
				Goodstable goodstable = UserMessUntil.getgoodstable(goods.get(i).getId());
				goodsNums[i].setVisible(true);
				goodsNums[i].setText(goods.get(i).getSum() + "");
				GoodsList[i].setIcon(GoodsListFromServerUntil.imgpath2(goodstable.getSkin()));
				getMouselistener()[i].setGoodstable(goodstable);
				if (EquipTool.isEquip(goodstable.getType())) {
					numMax+=goods.get(i).getSum();
				}else {
					numMax+=1;
				}
			}
		}
	}

	public void changeView() {
		for (int i = 0; i < 5; i++) {
			goodsNums[i].setText("");
			goodsNums[i].setVisible(false);
			GoodsList[i].setIcon(null);
		}
	}

	private ImageIcon iconBack;

	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);

		if (iconBack == null) {
			iconBack = CutButtonImage.getImage("inkImg/old/background/S11.png", -1, -1);
		}
		g.drawImage(iconBack.getImage(), 0, 0, 562, 60, this);

	}

	public JLabel[] getGoodsList() {
		if (GoodsList == null) {
			GoodsList = new JLabel[5];
			for (int i = 0; i < GoodsList.length; i++) {
				GoodsList[i] = new JLabel();
				GoodsList[i].setOpaque(false);
				GoodsList[i].setBounds(10 + i * 51, 8, 43, 43);
				GoodsList[i].addMouseListener(getMouselistener()[i]);
				this.add(GoodsList[i]);
			}
		}
		return GoodsList;
	}

	public void setGoodsList(JLabel[] goodsList) {
		GoodsList = goodsList;
	}

	public JLabel[] getGoodsNums() {
		if (goodsNums == null) {
			goodsNums = new JLabel[5];
			for (int i = 0; i < goodsNums.length; i++) {
				goodsNums[i] = new JLabel();
				goodsNums[i].setBounds(38 + i * 51, 6, 16, 16);
				goodsNums[i].setIcon(CutButtonImage.getImage("inkImg/old/background/S24.png", -1, -1));
				goodsNums[i].setVerticalTextPosition(SwingConstants.CENTER);
				goodsNums[i].setHorizontalTextPosition(SwingConstants.CENTER);
				goodsNums[i].setForeground(Color.GREEN);
				goodsNums[i].setFont(UIUtils.TEXT_FONT10);
				this.add(goodsNums[i]);
			}
		}
		return goodsNums;
	}

	public void setGoodsNums(JLabel[] goodsNums) {
		this.goodsNums = goodsNums;
	}

	public ImageIcon getIconBack() {
		return iconBack;
	}

	public void setIconBack(ImageIcon iconBack) {
		this.iconBack = iconBack;
	}

	public VipShopBtn getGetBtn() {
		if (getBtn == null) {
			getBtn = new VipShopBtn("inkImg/hongmu/1/lingqu.png", 1, 2, this);
			getBtn.setBounds(410, 17, 65, 26);
			this.add(getBtn);
		}
		return getBtn;
	}

	public void setGetBtn(VipShopBtn getBtn) {
		this.getBtn = getBtn;
	}

	public ChongjipackBean getChongjipackBean() {
		return chongjipackBean;
	}

	public void setChongjipackBean(ChongjipackBean chongjipackBean) {
		this.chongjipackBean = chongjipackBean;
	}

	public RichLabel getRichLabel() {
		if (richLabel == null) {
			richLabel = new RichLabel("#W连续充值#R21#W天", UIUtils.TEXT_FONT1);
			Dimension d = richLabel.computeSize(136);
			richLabel.setSize(d);
			richLabel.setPreferredSize(d);
			richLabel.setBounds(270, 15, 120, 40);
			this.add(richLabel);
		}
		return richLabel;
	}

	public void setRichLabel(RichLabel richLabel) {
		this.richLabel = richLabel;
	}

	public VipGoodsMouselistener[] getMouselistener() {
		if (mouselistener == null) {
			mouselistener = new VipGoodsMouselistener[5];
			for (int i = 0; i < mouselistener.length; i++) {
				mouselistener[i] = new VipGoodsMouselistener();
			}
		}
		return mouselistener;
	}

	public void setMouselistener(VipGoodsMouselistener[] mouselistener) {
		this.mouselistener = mouselistener;
	}

	public int getNumMax() {
		return numMax;
	}

	public void setNumMax(int numMax) {
		this.numMax = numMax;
	}

}
